package cc.ufinity.networkdiagnose.widget;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.view.R;

public class MoveOrOther extends GeneralTestWidget {

	private Activity ctx;
	private Button moveNet, otherNet;
	private WlanManager wlanManager;
	private Button backBtn;
	
	private boolean isOtherNet = false;
	private String networkType;
	private TelManager tm;
	
	public static final String TAG = "moveOrOther";
	public static final String CHINAMOBILE = "chinaMobile";
	public static final String OTHERNET = "otherNet";
	public MoveOrOther(Activity context) { 
		super(context);
		inflate(context, R.layout.move_or_other, this);
		this.ctx = context;
		moveNet = (Button)findViewById(R.id.move_net);
		otherNet = (Button)findViewById(R.id.other_net);
		backBtn = (Button) this.findViewById(R.id.test_btnback_moo);
		moveNet.setOnClickListener(listener);
		otherNet.setOnClickListener(listener);
		backBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	back(ctx);
            }
        });
		
		wlanManager = new WlanManager(ctx);
		wlanManager.setHandler(handler);
		tm = new TelManager(ctx);
	}
	
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MessageWhat.WIFI_RECEIVE_LIST:
				dismissMessageDialog();
				if(isOtherNet) {
					showOtherWifiListDialog(ctx, wlanManager, handler);
				} else {
					showOneKeyWifiListDialog(ctx, wlanManager, handler);
				} 
				wlanManager.stopScan();
				break;

			default:
				break;
			}
		}
	};
	
	OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			GlobalApp.taskOtherList.clear();
	    	GlobalApp.index = 0;
			if(v == moveNet) {
				if(GlobalApp.DEBUG) {
					isOtherNet = false;
					ctx.getIntent().putExtra(TAG,CHINAMOBILE);
					
					scanWifi();
				} else {
					networkType = tm.getNetworkType();
		        	if(networkType.equals("UNKNOW"))  {
		        		//未知网络
		        		showMessage(getContext(), "数据网络未知，请更正网络");
		        	} else if(networkType.equals("HSDPA")
		        			|| networkType.equals("EDGE") 
		        			|| networkType.equals("GPRS"))  {
		        		//移动网络
		        		isOtherNet = false;
						ctx.getIntent().putExtra(TAG,CHINAMOBILE);
						
						scanWifi();
		        	}else {
		        		//其他网络
		        		showMessage(getContext(), "手机数据网络不符合测试条件");
		        	}
				}
				
				/*TestWidget tw = new TestWidget(ctx);
				ctx.setContentView(tw);*/
			} else if(v == otherNet) {
				isOtherNet = true;
				ctx.getIntent().putExtra(TAG,OTHERNET);
				
				scanWifi();
				
				/*OtherNet on = new OtherNet(ctx);
				ctx.setContentView(on);*/
			}
		}
	};
	
	protected void scanWifi() {
		showMessageDialog(ctx, "正在扫描附近Wifi热点...");
		new Thread() {
			public void run() {
				wlanManager.startScan();
			}
		}.start();
	}

	
}
